<template>
  <div>
    <el-form v-if="formKeyList.length > 1" :inline="true" :model="form" class="demo-form-inline">
      <el-form-item v-for="(key ,index) in formKeyList" :key="'type' + key" :label="locationType == 'hn' ? '户' + (index + 1) : '楼' + (index + 1)" class="custom-form-item">
        <el-input :disabled="point.result === -1 || !point.ruleType" placeholder="请输入" v-model.number="form[key]" class="input-width" @change="handleChange(key)">
          <template slot="prepend">违反:</template>
          <template slot="append">{{ point.ruleUnit || '处' }}</template>
        </el-input>
      </el-form-item>
    </el-form>
    <el-form v-else :inline="true" :model="form" class="demo-form-inline" @submit.native.prevent>
      <el-form-item v-for="key in formKeyList" :key="'type' + key" class="custom-form-item">
        <el-input :disabled="point.result === -1 || !point.ruleType" placeholder="请输入" v-model.number="form[key]" class="input-width" @change="handleChange(key)">
          <template slot="prepend">违反:</template>
          <template slot="append">{{ point.ruleUnit || '处' }}</template>
        </el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import evaluation from '../evaluation'
export default {
  name: 'GgHnWlm',
  props: {
    locationType: {
      type: String,
      default: () => 'hn'
    },
    point: {
      type: Object,
      default: () => {
        return {
          result: 0,
          ruleType: 2
        }
      }
    }
  },
  data() {
    return {
      form: {},
      formKeyList: []
    }
  },
  watch: {
    point () {
      this.init()
    }
  },
  created() { // 初始化

  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      let count = 0
      if (this.point.deductCount && Number.isInteger(this.point.deductCount)) {
        count = this.point.deductCount
      } else {
        this.point.deductCount = 1
        count = 1
      }
      const dataForm = {}
      for (let i = 0; i < count; i++) {
        const type = this.locationType === 'hn' ? 'room' : 'build'
        dataForm[type + i] = null
      }
      let form = {}
      if (this.point.deductJson) {
        try {
          form = JSON.parse(this.point.deductJson)
        } catch (e) {
          console.error(e)
        }
      }
      Object.assign(dataForm, form)
      this.form = dataForm
      this.formKeyList = Object.keys(this.form)
    },
    handleChange(key) {
      if (this.form[key] && !Number(this.form[key])) {
        this.$message.error('数量请输入数字！')
        this.form[key] = null
        return
      }
      if (this.form[key] < 0 || this.form[key] > 999) {
        this.$message.error('请输入在 0 ~ 999范围内数量！')
        this.form[key] = null
        return
      }
      if (this.form[key] < 1 && this.form[key] > 0) {
        this.form[key] = parseInt(this.form[key])
      }
      let itemScore = 0
      const keys = Object.keys(this.form)
      let filterKeys = []
      if (this.locationType === 'hn') {
        filterKeys = keys.filter(key => key.includes('room'))
      } else if (this.locationType === 'wlm') {
        filterKeys = keys.filter(key => key.includes('build'))
      }
      let ruleJson = []
      try {
        ruleJson = JSON.parse(this.point.ruleJson)
      } catch (e) {
        console.log(e)
      }
      const list = []
      if (ruleJson && ruleJson instanceof Array && ruleJson.length > 0) {
        for (let i = 0; i < filterKeys.length; i++) {
          list.push({ badTotal: this.form[filterKeys[i]], score: 0, result: 0, ruleType: this.point.ruleType })
        }
        for (let j = 0; j < list.length; j++) {
          const data = list[j]
          if (data.ruleType === 6) { // 阈值扣分 （阈值范围，扣分）
            evaluation.rangeReduceScore(data, ruleJson)
          } else if (data.ruleType === 7) { // 重复扣分 （（badTotal - min) * score），扣分）
            evaluation.repetiteReduceScore(data, ruleJson)
          } else if (data.ruleType === 8) { // 直接扣分 （超过阈值 扣相同分）
            evaluation.directReduceScore(data, ruleJson)
          }
          itemScore += list[j].score
        }
      }

      // 计算结果
      let result = 10
      for (let i = 0; i < list.length; i++) {
        if (list[i].result === 11) {
          result = 11
        }
      }

      let badTotal = 0
      let allBad = true // 是否全填写
      for (const key in this.form) {
        badTotal += this.form[key]
        if (!this.form[key] && this.form[key] !== 0) {
          allBad = false
        }
      }
      if (result === 10) { // 合格
        badTotal = 0
      }
      const avaScore = itemScore / this.point.deductCount
      this.$emit('change', { form: this.form, score: avaScore, result: result, badTotal: allBad ? badTotal : null })
    }
  }
}
</script>

<style scoped lang='scss'>
  .custom-form-item {
    margin-right: 30px;
    .input-width {
      width: 230px;
    }
  }

</style>
